package com.sunwhite.kaikeba.utils;

import com.sunwhite.kaikeba.utils.http.HttpUtils;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class WeixinHttpUtil {
    private final static Logger logger= LoggerFactory.getLogger(WeixinHttpUtil.class);



    //获取token
    private final static String GET_TOKEN_URL="https://api.weixin.qq.com/sns/jscode2session";
    //appid
    private final static String APP_ID="wx9072e31d794beb4c";
    private final static String app_secret="2dbf50ee85f8b420c831bb7d86ae1ee4";



    public static Map<String,Object> getAccessToken(String code){
        Map<String, String> query = new HashMap<>();
        query.put("appid",APP_ID);
        query.put("secret",app_secret);
        //authorization_code client_credential
        query.put("grant_type","authorization_code");
        query.put("js_code",code);
        Map<String, Object> resultMap=null;
        try {
              HttpResponse httpResponse = HttpUtils.doGet(GET_TOKEN_URL, query);
              resultMap= HttpUtils.getResultMap(httpResponse);
              logger.info("微信请求结果为：{}",resultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
